package com.ruoyi.web.controller.crm;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.CrmStudent;
import com.ruoyi.system.domain.CrmStudentVo;
import com.ruoyi.system.service.ICrmStudentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学员信息Controller
 * 
 * @author Bill
 * @date 2024-06-22
 */
@Controller
@RequestMapping("/crm/salesman/student")
public class CrmSalesmanController extends BaseController
{
    private String prefix = "crm/salesman/student";

    @Autowired
    private ICrmStudentService crmStudentService;

    @RequiresPermissions("system:student:view")
    @GetMapping()
    public String student()
    {
        return prefix + "/student";
    }

    /**
     * 查询学员信息列表
     */
    @RequiresPermissions("system:student:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CrmStudent crmStudent)
    {
        crmStudent.setSalesmanId(getUserId());
        startPage();
        List<CrmStudent> list = crmStudentService.selectCrmStudentList(crmStudent);
        TableDataInfo rspData = getDataTable(list);
        crmStudentService.selectCrmStudentVoList(rspData);
        return rspData;
    }

    /**
     * 导出学员信息列表
     */
    @RequiresPermissions("system:student:export")
    @Log(title = "学员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CrmStudent crmStudent)
    {
        List<CrmStudent> list = crmStudentService.selectCrmStudentList(crmStudent);
        ExcelUtil<CrmStudent> util = new ExcelUtil<CrmStudent>(CrmStudent.class);
        return util.exportExcel(list, "学员信息数据");
    }

    /**
     * 新增学员信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("users", crmStudentService.selectUserList(new SysUser()));
        return prefix + "/add";
    }

    /**
     * 新增保存学员信息
     */
    @RequiresPermissions("system:student:add")
    @Log(title = "学员信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CrmStudent crmStudent)
    {
        crmStudent.setCreateUser(getUserId());
        return toAjax(crmStudentService.insertCrmStudent(crmStudent));
    }

    /**
     * 修改学员信息
     */
    @RequiresPermissions("system:student:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CrmStudent crmStudent = crmStudentService.selectCrmStudentById(id);
        mmap.put("crmStudent", crmStudent);
        mmap.put("users", crmStudentService.selectUserList(new SysUser()));

        return prefix + "/edit";
    }

    /**
     * 修改保存学员信息
     */
    @RequiresPermissions("system:student:edit")
    @Log(title = "学员信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CrmStudent crmStudent)
    {
        crmStudent.setUpdateUser(getUserId());
        return toAjax(crmStudentService.updateCrmStudent(crmStudent));
    }

    /**
     * 删除学员信息
     */
    @RequiresPermissions("system:student:remove")
    @Log(title = "学员信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(crmStudentService.deleteCrmStudentByIds(ids));
    }


    /**
     * 学员跟进状态修改
     */
    @Log(title = "学员管理-跟踪状态", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:student:edit")
    @PostMapping("/changeFollowStatus")
    @ResponseBody
    public AjaxResult changeFollowStatus(CrmStudent student)
    {
//        userService.checkUserAllowed(user);
//        userService.checkUserDataScope(user.getUserId());
        return toAjax(crmStudentService.changeFollowStatus(student));
    }
}
